package com.gxa.autoparts.admin.controller;

import com.gxa.autoparts.admin.service.OrderService;
import com.gxa.autoparts.core.common.ExceptionCode;
import com.gxa.autoparts.core.common.JsonResult;
import com.gxa.autoparts.core.exception.BusinessException;
import com.gxa.autoparts.core.pojo.AfterSale;
import com.gxa.autoparts.core.pojo.Order;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

@RequestMapping("/admin/order")
@RestController
@Transactional(rollbackFor = Throwable.class)
public class OrderController {

    @Autowired
    private OrderService orderService;

    @ApiOperation("查询全部订单")
    @PostMapping("/findAllSystemMessage")
    @ApiImplicitParams({

            @ApiImplicitParam(name = "seriaLnNumber", value = "订单编号", required = false),
            @ApiImplicitParam(name = "state", value = "订单状态", required = false),
            @ApiImplicitParam(name = "userName", value = "用户姓名", required = false),
            @ApiImplicitParam(name = "phone", value = "手机号", required = false),
            @ApiImplicitParam(name = "time1", value = "起始时间", required = false),
            @ApiImplicitParam(name = "time2", value = "截止时间", required = false),
            @ApiImplicitParam(name = "page", value = "页数", required = true),
            @ApiImplicitParam(name = "size", value = "页内数据条数", required = true),
    })
    public Map findAllSystemMessage(String seriaLnNumber,String state,String userName,String phone,
                                    String time1,String time2,Integer page,Integer size) throws BusinessException {

        List<Map> map = orderService.selectAllOrder(seriaLnNumber, state, userName, phone, time1, time2, (page - 1) * size, size);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setData(map);
        jsonResult.put("totleNumber",orderService.selectAllOrder2(seriaLnNumber, state, userName, phone, time1, time2).size());
        jsonResult.setMsg("查询全部订单成功");
        return jsonResult.getDataMap();
    }

    @ApiOperation("订单操作")
    @PostMapping("/updateOrderState")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "订单Id", required = true),
            @ApiImplicitParam(name = "state", value = "订单状态", required = true)
    })
    public Map updateOrderState(Long id, Integer state) throws BusinessException {
        Order order = orderService.selectById(id);
        if (1>=state&&state>=5){
            throw new BusinessException(ExceptionCode.WRONG_STATE);
        }
        order.setState(state);
        orderService.updateById(order);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("订单操作成功");
        return jsonResult.getDataMap();
    }

    @ApiOperation("订单详情")
    @PostMapping("/selectOrderByid")
    @ApiImplicitParam(name = "id", value = "订单Id", required = true)
    public Map selectOrderByid(Long id){
        Map map = orderService.selectOrderById(id);
        List<Map> maps = orderService.selectGoodsById(id);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setData(map);
        jsonResult.put("GoodsDate",maps);
        jsonResult.setMsg("订单详情查询成功");

        return jsonResult.getDataMap();
    }

    /*@GetMapping("/export/platMoney")
    @ApiOperation("导出记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "流水单号",required = false),
            @ApiImplicitParam(name="type",value = "资金类型",required = false),
            @ApiImplicitParam(name="outOrIn",value = "出入账",required = false)
    })
    public void exportPlatformMoney(HttpServletResponse response,
                                    Long id,
                                    String type,
                                    String outOrIn) throws IOException {
        //要导出的数据
        List<Platform> allPlatForm = platFormService.findAllPlatForm(id, type, outOrIn);
        // 工作簿对象
        HSSFWorkbook workbook = new HSSFWorkbook();
        // 创建工作表
        HSSFSheet sheet = workbook.createSheet("平台资金表");
        String fileName = "平台资金表" + ".xls";
        // 将文件名中的中文部分被URL编码后的内容进行解析, 最终还是显示为中文状态
        fileName = URLEncoder.encode(fileName, "utf-8").replaceAll("\\+", "%20");
        // headers表示excel表中第一行的表头 在excel表中添加表头
        String[] headers = {"流水号", "类型", "出入账", "操作金额", "手续费", "操作前余额", "操作后余额", "状态", "备注", "操作时间"};
        // 创建一个行对象, 作为表头
        HSSFRow row = sheet.createRow(0);
        for (int i = 0; i < headers.length; i++) {
            HSSFCell cell = row.createCell(i);   // 在行中添加单元格
            HSSFRichTextString text = new HSSFRichTextString(headers[i]);
            cell.setCellValue(text);   // 把字段名添加到表头行上
        }
        //准备将时间格式处理为标准日期格式
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // 添加数据行:
        int rowNum = 1;
        for (Platform item : allPlatForm) {
            HSSFRow dataRow = sheet.createRow(rowNum);
            if (item.getId() == null) {
                dataRow.createCell(0).setCellValue("");
            } else {
                dataRow.createCell(0).setCellValue(item.getId());
            }

            if (item.getType() == null) {
                dataRow.createCell(1).setCellValue("");
            } else {
                dataRow.createCell(1).setCellValue(item.getType());
            }

            if (item.getOutOrIn() == null) {
                dataRow.createCell(2).setCellValue("");
            } else {
                dataRow.createCell(2).setCellValue(item.getOutOrIn());
            }

            if (item.getHandleMoney() == null) {
                dataRow.createCell(3).setCellValue("");
            } else {
                dataRow.createCell(3).setCellValue(item.getHandleMoney());
            }

            if (item.getPoundage() == null) {
                dataRow.createCell(4).setCellValue("");
            } else {
                dataRow.createCell(4).setCellValue(item.getPoundage());
            }

            if (item.getBeforeHandle() == null) {
                dataRow.createCell(5).setCellValue("");
            } else {
                dataRow.createCell(5).setCellValue(item.getBeforeHandle());
            }

            if (item.getAfterHandle() == null) {
                dataRow.createCell(6).setCellValue("");
            } else {
                dataRow.createCell(6).setCellValue(item.getAfterHandle());
            }

            if (item.getState() == null) {
                dataRow.createCell(7).setCellValue("");
            } else {
                dataRow.createCell(7).setCellValue(item.getState());
            }

            if (item.getRemarks() == null) {
                dataRow.createCell(8).setCellValue("");
            } else {
                dataRow.createCell(8).setCellValue(item.getRemarks());
            }

            if (item.getHandleTime() == null) {
                dataRow.createCell(9).setCellValue("");
            } else {
                dataRow.createCell(9).setCellValue(item.getHandleTime());
            }
            rowNum++;
        }
        //返回给前端下载
        response.setContentType("application/octet-stream");
        //附件格式打卡，不是打开页面
        response.setHeader("Content-disposition", "attachment;filename=" + fileName);
        response.flushBuffer();
        workbook.write(response.getOutputStream());
    }*/




}
